<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\CodeValueRepository;
use App\Utils\ViewHelper;
use Illuminate\Http\Request;
use App\Http\Requests\CodeValueSaveRequest;

/**
 * 控制器
 * Class CodeValueController
 * @package App\Http\Controllers\Admin
 */
class CodeValueController extends BaseController
{
    private $rep;

    public function __construct(CodeValueRepository $rep)
    {
        $this->set('code/value');
        $this->rep = $rep;
    }

    function index($code, Request $request)
    {
        $page = $this->rep->adminPager($code, $request->all());
        return $this->view('index', compact('page','code'));
    }

    function create($code)
    {
        $value = $this->rep->value($code);
        return $this->view('create')->with('code', $code)->with('value', $value);
    }

    function save(CodeValueSaveRequest $request)
    {
        $data = $request->all();
        $id = $request->get('id', 0);
        return $this->rep->save($data, $id);
    }

    function edit($id)
    {
        $result = $this->rep->editInfo($id);
        if ($result->isSuccess()) {
            return $this->view('edit')->with('entity', $result->data);
        }
        return ViewHelper::E404($result->msg);
    }

    function destroy($id)
    {
        return $this->rep->destroy($id);
    }
}
